package com.ex.giaodien.XulySukien;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.plaf.PanelUI;
public class CongTruNhanChiaUI extends JFrame {
	JButton btnThoat, btnCong, btnTru, btnNhan, btnChia, btnXoa;
	private static final long serialVersionUID = 1L;
	public CongTruNhanChiaUI(String title)
	{
		setTitle(title);
	}
	public void doShow()
	{
		setSize(400, 300);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		addControl();
		setResizable(false);
		setVisible(true);
	}	
	public void addControl()
	{
		JPanel pnBorder=new JPanel();
		pnBorder.setLayout(new BorderLayout());
		JPanel pnNorth=new JPanel();
		JLabel lblTitle=new JLabel("Cộng Trừ Nhân Chia");
		pnNorth.add(lblTitle);
		pnBorder.add(pnNorth,BorderLayout.NORTH);
		lblTitle.setForeground(Color.BLUE);
		Font ft=new Font("arial", Font.BOLD, 25);
		lblTitle.setFont(ft);
			
		JPanel pnSouth=new JPanel();
		pnSouth.setPreferredSize(new Dimension(0, 30));
		pnSouth.setBackground(Color.PINK);
		JPanel pns1=new JPanel();
		pns1.setBackground(Color.BLUE);
		pnSouth.add(pns1);
		JPanel pns2=new JPanel();
		pns2.setBackground(Color.RED);
		pnSouth.add(pns2);
		JPanel pns3=new JPanel();
		pns3.setBackground(Color.YELLOW);
		pnSouth.add(pns3);
		pnBorder.add(pnSouth,BorderLayout.SOUTH);
		
		JPanel pnCenter=new JPanel();
		pnCenter.setLayout(new BoxLayout(pnCenter, BoxLayout.Y_AXIS));
		pnBorder.add(pnCenter,BorderLayout.CENTER);
		
		Border  centerborder
		=BorderFactory.createLineBorder(Color.RED);
		TitledBorder centerTitleBorder=
				new TitledBorder(centerborder, "nhập 2 số a và b:");
		pnCenter.setBorder(centerTitleBorder);

		JPanel pna=new JPanel();
		JLabel lbla=new JLabel("nhập a:");
		final JTextField txta=new  JTextField(15);
		pna.add(lbla);
		pna.add(txta);
		pnCenter.add(pna);
		
		JPanel pnb=new JPanel();
		JLabel lblb=new JLabel("nhập b:");
		final JTextField txtb=new  JTextField(15);
		pnb.add(lblb);
		pnb.add(txtb);
		pnCenter.add(pnb);
		
		JPanel pnkq=new JPanel();
		JLabel lblkq=new JLabel("Kết quả:");
		final JTextField txtkq=new  JTextField(15);
		pnkq.add(lblkq);
		pnkq.add(txtkq);
		pnCenter.add(pnkq);
		
		JPanel pnc=new JPanel();
		JPanel pnpheptoan=new JPanel();
		pnpheptoan.setLayout(new GridLayout(1, 4));
		pnpheptoan.setBorder(new TitledBorder(BorderFactory.createLineBorder(Color.BLACK),"Chọn phép toán:"));
		
		JButton btnCong=new JButton("Cộng");
		pnpheptoan.add(btnCong);
		final JButton btnTru=new JButton("Trừ");
		pnpheptoan.add(btnTru);
		final JButton btnNhan=new JButton("Nhân");
		pnpheptoan.add(btnNhan);
		final JButton btnChia=new JButton("Chia");
		JButton btnThoat=new JButton("Thoát");
		pnpheptoan.add(btnChia);
		ButtonGroup group=new ButtonGroup();
		group.add(btnCong);group.add(btnTru);
		group.add(btnNhan);group.add(btnChia);
		
		pnc.add(pnpheptoan);
		pnCenter.add(pnc);
		
	
		
		lbla.setPreferredSize(lblkq.getPreferredSize());
		lblb.setPreferredSize(lblkq.getPreferredSize());
		
		btnThoat.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int ret=JOptionPane.showConfirmDialog(null, "Muốn thoát hả?", "Thoát", JOptionPane.YES_NO_OPTION);
				if(ret==JOptionPane.YES_OPTION)
					System.exit(0);
			}
		});
			
		btnCong.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String sa=txta.getText();
				int a=0,b=0;
				try
				{
					a=Integer.parseInt(sa);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
					txta.selectAll();
					txta.requestFocus();
					return;
				}
				String sb=txtb.getText();
				try
				{
					b=Integer.parseInt(sb);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
					txtb.selectAll();
					txtb.requestFocus();
					return;
				}
				double kq=0;
				
					kq=(a+b);
								
				txtkq.setText(kq+"");
			}
		});
		btnTru.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String sa=txta.getText();
				int a=0,b=0;
				try
				{
					a=Integer.parseInt(sa);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
					txta.selectAll();
					txta.requestFocus();
					return;
				}
				String sb=txtb.getText();
				try
				{
					b=Integer.parseInt(sb);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
					txtb.selectAll();
					txtb.requestFocus();
					return;
				}
				double kq=0;
				
					kq=(a-b);
								
				txtkq.setText(kq+"");
			}
		});
		btnNhan.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String sa=txta.getText();
				int a=0,b=0;
				try
				{
					a=Integer.parseInt(sa);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
					txta.selectAll();
					txta.requestFocus();
					return;
				}
				String sb=txtb.getText();
				try
				{
					b=Integer.parseInt(sb);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
					txtb.selectAll();
					txtb.requestFocus();
					return;
				}
				double kq=0;
				
					kq=(a*b);
								
				txtkq.setText(kq+"");
			}
		});
		btnChia.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String sa=txta.getText();
				int a=0,b=0;
				try
				{
					a=Integer.parseInt(sa);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
					txta.selectAll();
					txta.requestFocus();
					return;
				}
				String sb=txtb.getText();
				try
				{
					b=Integer.parseInt(sb);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, "Nhập sai định dạng!");
					txtb.selectAll();
					txtb.requestFocus();
					return;
				}
				double kq=0;
				try {
					kq=(a/b);
				} catch (Exception e) {
					// TODO: handle exception

					JOptionPane.showMessageDialog(null, "Không thể chia được cho 0!");
					txtb.selectAll();
					txtb.requestFocus();
					return;
				}
			
								
				
								
				txtkq.setText(kq+"");
			}
		});
		Container con=getContentPane();
		con.add(pnBorder);
	}
	
}
